package com.ddone.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author ddone
 * @date 2024/3/22-12:21
 */
@Controller
@RequestMapping("/share")
public class DataShareController {
    @Autowired(required = false)
    private ServletContext servletContext;

    /**
     * 放入到request共享域 使用springMvc封装的
     * model  modelMap  map modelAndView
     */

    @GetMapping("/model")
    public String testModel(Model model) {
        model.addAttribute("req", "model Data");
        return "hello";
    }

    @GetMapping("/model-map")
    public String testModelMap(ModelMap modelMap) {
        modelMap.addAttribute("req", "ModelMap Data");
        return "hello";
    }

    @GetMapping("/map")
    public String testMap(Map<String, Object> map) {
        map.put("req", "Map Data");
        return "hello";
    }

    @GetMapping("/model-and-view")
    public ModelAndView testModelAndView() {
        ModelAndView modelAndView = new ModelAndView().addObject("req", "modelAndView Data");
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @GetMapping("/api")
    public String testServletApi(HttpServletRequest request, HttpSession session){
        request.setAttribute("req","Servlet Data");
        session.setAttribute("ses","Session Data");
        return "hello";
    }

}
